﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Thauan_Juliano_World
{
    public class Plano : WorldObject
    {

        private Color normalColor = Color.White;
        private Color obstacleColor = Color.Turquoise;
        private Color pathColor = Color.Orange;
        private Color scannedColor = Color.Violet;
        private Color originColor = Color.Green;
        private Color goalColor = Color.Gold;

        private PlaneType type = PlaneType.Normal;

        private GameObject plane;
        private VertexPositionColor[] verts;

        private Vector3 position;
        private Vector3 scale;

        private List<GameObject> gameObjects = new List<GameObject>();

        private List<WorldObject> mainGameObjectsList;

        private BasicEffect effect;
        private GraphicsDevice gd;

        public Plano(List<WorldObject> mainGameObjectsList, Vector3 position, Vector3 scale)
        {
            this.mainGameObjectsList = mainGameObjectsList;
            this.gd = Inventory.getGraphicsDevice();
            this.effect = Inventory.getBasicEffect();

            this.position = position;
            this.scale = scale;

            // --------------------------------- Suporte ----------------------------------- //

            this.verts = new VertexPositionColor[4];

            float sizeX = this.scale.X / 2;
            float sizeZ = this.scale.Z / 2;

            verts[0] = new VertexPositionColor(new Vector3(-sizeX, 0, -sizeZ), Color.White);
            verts[1] = new VertexPositionColor(new Vector3(-sizeX, 0, sizeZ), Color.White);
            verts[2] = new VertexPositionColor(new Vector3(sizeX, 0, -sizeZ), Color.White);
            verts[3] = new VertexPositionColor(new Vector3(sizeX, 0, sizeZ), Color.White);

            this.plane = new GameObject(verts, 0, verts.Length - 2, gameObjects, Matrix.Identity, new Vector3(position.X, 5 + position.Y, position.Z));

            // ========:> Add world object
            this.mainGameObjectsList.Add(this);

        }

        public void ChangeType(PlaneType type) 
        {
            this.type = type;
            Color color = Color.White;
            switch (type) 
            {
                case PlaneType.Normal:
                    color = this.normalColor;
                    break;
                case PlaneType.Obstacle:
                    color = this.obstacleColor;
                    break;
                case PlaneType.Scanned:
                    color = this.scannedColor;
                    break;
                case PlaneType.Path:
                    color = this.pathColor;
                    break;
                case PlaneType.Origin:
                    color = this.originColor;
                    break;
                case PlaneType.Goal:
                    color = this.goalColor;
                    break;
            }

            for (int i = 0; i < this.verts.Length; i++)
            {
                this.verts[i].Color = color;
            }
        }
        
        public override void Update()
        {

        }

        public override void Draw()
        {
            foreach (GameObject go in gameObjects)
            {
                go.SetPosition();
                effect.World = go.world;

                go.SetForWath();

                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Apply();

                    this.gd.DrawUserPrimitives<VertexPositionColor>
                    (go.pt, go.verts, go.offset, go.count);
                }
                
                go.ResetPosition();
            }
        }
        public override void Destroy() 
        {
            try { this.mainGameObjectsList.Remove(this); }
            catch (Exception e) 
            {
                Console.WriteLine("Erro: " + e);
            }
        }
    }
}
